package edu.washington.vaccine.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import edu.washington.vaccine.R;

/*
 * Tab option from Main Activity
 * Displays two options: add patient and find patient
 */
public class VaccineActivity extends Activity {
	@SuppressWarnings("unused")
	private static final String TAG = "VaccineActivity";
	protected static LocalActivityManager mLocalActivityManager;
	Button launchButton;
	private Context context;
	private ArrayList<String> mIdList;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
       	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.choose_search);
        this.context = this;
        if (mIdList == null) mIdList = new ArrayList<String>();
       
        Button registerButton = (Button) findViewById(R.id.newpatient_button);
        registerButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		Intent i = new Intent(context, AddPatientActivity.class);
//        		replaceContentView("register", i);
        		FirstGroup.group.addChildActivity("register", i);
        	}
        });
		
		Button searchButton = (Button) findViewById(R.id.findpatient_button);
		searchButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(context, SearchPatientActivity.class);
//				replaceContentView("search", i);
				FirstGroup.group.addChildActivity("search", i);
			}
		});
    }
    
//    public void finishFromChild(Activity child) {
//    	LocalActivityManager manager = getLocalActivityManager();
//    	int index = mIdList.size()-1;
//
//    	if (index < 1) {
//    		finish();
//    		return;
//    	}
//
//    	manager.destroyActivity(mIdList.get(index), true);
//    	mIdList.remove(index);
//    	index--;
//    	String lastId = mIdList.get(index);
//    	Intent lastIntent = manager.getActivity(lastId).getIntent();
//    	Window newWindow = manager.startActivity(lastId, lastIntent);
//    	setContentView(newWindow.getDecorView());
//    }
    
//    public void replaceContentView(String id, Intent newIntent) {
//    	View view = FirstGroup.group.getLocalActivityManager().startActivity(id, newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
//    	FirstGroup.group.setContentView(view);
//    	
////    	Window window = getLocalActivityManager().startActivity(id, newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
////    	if (window != null) {
////    		mIdList.add(id);
////    		setContentView(window.getDecorView());
////    	}
//    }
    
    @Override
	public void onBackPressed() {
//		Intent i = new Intent(this, VaccineActivity.class);
//		this.replaceContentView("home", i);
		
//    	int length = mIdList.size();
//    	if (length > 1) {
//    		Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
//    		current.finish();
//    	}
    	FirstGroup.group.back();
    }
}
